package ctx

import (
	"fmt"
	"go-gin-init/app/models/resp"
	"go-gin-init/common/xerr"

	"github.com/gin-gonic/gin"
)

// GetLoginUserCtx 获取loginUser id
func GetLoginUserCtx(ctx *gin.Context) (int64, error) {
	value, exists := ctx.Get("loginUser")
	if !exists {
		return 0, xerr.NewErrCode(xerr.NOT_LOGIN)
	}
	claims := value.(resp.BaseClaims)
	return claims.ID, nil
}

func CheckPermissionCtx(id int64, ctx *gin.Context) error {

	value, exists := ctx.Get("loginUser")
	if !exists {
		return xerr.NewErrCode(xerr.NOT_LOGIN)
	}
	claims := value.(resp.BaseClaims)

	fmt.Println(id)
	fmt.Println(claims.ID)

	if id != claims.ID {
		return xerr.NewErrCode(xerr.NOT_LOGIN)
	}

	return nil

}
